[pink~] pink noise generator
Generates pink noise with equal energy per octave

Outlet: pink noise

For more info please see this page...
Download
pink~.zip - object, help patch, source code

Source Code
//------------------------------------------------------------------------------
//  Pink Noise Object for Pd
//
//  pink~.c
//
//  Generates pink noise using a series of summed and attenuated lowpass filters
//
//  Created by Cooper Baker on 2/4/15.
//  Copyright (c) 2015 Cooper Baker. All rights reserved.
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
// headers
//------------------------------------------------------------------------------

// main header for pd
#include "m_pd.h"

// standard library for random number generator
#include <stdlib.h>

// time header to seed random numbers
#include <time.h>

// utility header for Pd Spectral Toolkit project
#include "utility.h"

// disable compiler warnings on windows
#ifdef NT
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif


//------------------------------------------------------------------------------
// pink_class - pointer to this object's definition
//------------------------------------------------------------------------------
static t_class* pink_class;


//------------------------------------------------------------------------------
// pink - data structure holding this object's data
//------------------------------------------------------------------------------
typedef struct pink
{
    // this object - must always be first variable in struct
    t_object object;
   
    // filter coefficients
    double a[ 8 ];

    // filter gains
    double g[ 8 ];
   
    // filter states
    double y[ 8 ];

    // filter gain
    double gain;
   
    // number of filters
    t_int filters;
   
    // random normalization coefficient
    double rand_norm;
   
} t_pink;


//------------------------------------------------------------------------------
// function prototypes
//------------------------------------------------------------------------------
static t_int* pink_perform     ( t_int* io );
static void   pink_dsp         ( t_pink* object, t_signal **sig );
static void*  pink_new         ( void );
void          pink_tilde_setup ( void );


//------------------------------------------------------------------------------
// pink_perform - the signal processing function of this object
//------------------------------------------------------------------------------
static t_int* pink_perform( t_int* io )
{
    // store variables from dsp input/output array
    t_float* out    = ( t_float* )( io[ 1 ] );
    t_int    frames = ( t_int    )( io[ 2 ] );
    t_pink*  object = ( t_pink*  )( io[ 3 ] );

    // copy pointers to filter value arrays
    double* a = object->a;
    double* g = object->g;
    double* y = object->y;
   
    // copy filter parameters
    double gain    = object->gain;
    t_int  filters = object->filters;
   
    // random number variable
    double rand_norm = object->rand_norm;

    // signal vector iterator variable
    t_int n = -1;
   
    // filter iterator variable
    t_int i;
   
    // noise variables
    double white;
    double pink;
   
    // the dsp loop
    while( ++n < frames )
    {
        // make white noise normalized between -1 and 1
        white = rand() * rand_norm - 1.0;

        // initialize
        pink  = 0.0;
        i     = -1;

        // the filter loop
        while( ++i < filters )
        {
            // filter the white noise
            y[ i ] = a[ i ] * white + ( 1.0 - a[ i ] ) * y[ i ];
           
            // apply gain and accumulate filtered noise
            pink += y[ i ] * g[ i ];
        }
       
        // apply overall gain and copy to output
        out[ n ] = pink * gain;
    }

    // return the dsp input/output array address plus one more than its size
    // to provide a pointer to the next perform function in pd's call list
    return &( io[ 4 ] );
}


//------------------------------------------------------------------------------
// pink_dsp - installs this object's dsp function in pd's callback list
//------------------------------------------------------------------------------
static void pink_dsp( t_pink* object, t_signal **sig )
{
    // get sample rate
    t_float sr = sig[ 0 ]->s_sr;

    // copy pointers to filter value arrays
    double* a = object->a;
    double* g = object->g;

    // allocate temporary variables
    t_int  i       = 0;
    t_int  db      = 0;
    double amp_sum = 0;
   
    // calculate maximum cutoff frequency so that filter coefficient < 1.0;
    double freq = ( sr / C_2_PI ) - 1.0;
   
    // calculate coefficients
    while( freq > 1 )
    {
        a[ i ] = C_2_PI * freq / sr;
        freq   = freq / 4.0;
       
        i++;
    }

    // store number of filters
    object->filters = i;
   
    // calculate gains
    while( i-- > 0 )
    {
        g[ i ]   = DbToA( db );
        amp_sum += DbToA( db );
        db      -= 6.0;
    }
   
    // calculate overall gain
    object->gain = DbToA( -AToDb( amp_sum ) );
   
    // dsp_add arguments
    //--------------------------------------------------------------------------
    // perform routine
    // number of passed parameters
    // outlet sample vector
    // sample frames to process (vector size)
    // object pointer
    dsp_add( pink_perform, 3, sig[ 0 ]->s_vec, sig[ 0 ]->s_n, object );
}


//------------------------------------------------------------------------------
// pink_new - instantiates a copy of this object in pd
//------------------------------------------------------------------------------
static void* pink_new( void )
{
    // create a pointer to this object
    t_pink* object = ( t_pink* )pd_new( pink_class );
   
    // create a new signal outlet for this object
    outlet_new( &object->object, gensym( "signal" ) );
   
    // calculate 2 over rand max
    object->rand_norm = ( 2.0 / RAND_MAX );

    // initialize filter values
    memset( object->a, 0, 8 * sizeof( double ) );
    memset( object->g, 0, 8 * sizeof( double ) );
    memset( object->y, 0, 8 * sizeof( double ) );
   
    // seed the random number generator
    time_t t;
    srand( ( unsigned )time( &t ) );
   
    return object;
}


//------------------------------------------------------------------------------
// pink_tilde_setup - describes the attributes of this object to pd so it may be properly instantiated
// (must always be named with _tilde replacing ~ in the object name)
//------------------------------------------------------------------------------
void pink_tilde_setup( void )
{
    // creates an instance of this object and describes it to pd
    pink_class = class_new( gensym( "pink~" ), ( t_newmethod )pink_new, 0, sizeof( t_pink ), CLASS_NOINLET, 0 );
   
    // installs pink_dsp so that it will be called when dsp is turned on
    class_addmethod( pink_class, ( t_method )pink_dsp, gensym( "dsp" ), 0 );
   
    // announce this object in the pd console
    Announce( "pink~: pink noise generator - v1.0" );
}


//------------------------------------------------------------------------------
// EOF
//------------------------------------------------------------------------------